<template>
  <div class="box">
    <p class="goback" @click="gobackFn"> <i class="el-icon-back"></i> | 返回</p>
    <header>
      <p class="title">确认收货地址</p>
      <div class="address" :class="{ addressactive : item.id === activeId}" @click="changeclass(item.id)" v-for="item in addressList" :key="item.id">
        <i class="el-icon-close deleteicon" @click.stop="deleteaddressFn(item.id)"></i>
        <span>收货人：</span><span>{{item.name}}</span><br>
        <span>手机号码：</span><span>{{item.phone}}</span><br>
        <span>收货地址：</span><span>{{item.address}}</span>
      </div>
      <div class="address addaddress">
        <div @click="addaddressFn">
          <img src="../../assets/addaddress.png" alt="" class="imgaddress">
          <p>新增收获地址</p>
        </div>
        <el-dialog title="收货地址" :visible.sync="dialogFormVisible" :modal="false"  @close="dialogCloseFN">
          <el-form :model="addressForm" ref="addref" :rules="addressRules">
            <el-form-item label="收件人：" :label-width="formLabelWidth" prop="name">
              <el-input v-model="addressForm.name" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="收件号码：" :label-width="formLabelWidth" prop="phone">
              <el-input v-model="addressForm.phone" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="收件地址：" :label-width="formLabelWidth" prop="address">
              <el-input type="textarea" v-model="addressForm.address" autocomplete="off"></el-input>
            </el-form-item>
          </el-form>
          <div slot="footer" class="dialog-footer">
            <el-button @click="dialogFormVisible = false">取 消</el-button>
            <el-button type="primary" @click="cofirmCommit">确 定</el-button>
          </div>
        </el-dialog>
      </div>
    </header>
    <main>
      <p class="title">确认订单信息</p>
      <el-table
        ref="multipleTable"
        :data="orderList"
        tooltip-effect="dark"
        style="width: 100%"
        >
        <el-table-column
          label="序号"
          type="index"
          width="50">
        </el-table-column>
        <el-table-column
          label="商品信息"
          width="380">
          <template slot-scope="scope">
            <img :src="scope.row.product_img" alt="" class="shoppingcatstoreImg">
            <span class="storeinfo">{{ scope.row.product_name }}</span>
          </template>
        </el-table-column>
        <el-table-column
          label="单价"
          width="140">
          <template slot-scope="scope">
            <p v-if="scope.row.price.indexOf('.') !== -1">{{ scope.row.price }}</p>
            <p v-else>{{ scope.row.price }}.00</p>
          </template>
        </el-table-column>
        <el-table-column
          prop="num"
          label="数量"
          width="140">
        </el-table-column>
        <el-table-column
          label="小计"
          width="140">
          <template slot-scope="scope">
            <p style="color:red;" v-if="scope.row.price.indexOf('.') !== -1">￥{{ parseFloat(scope.row.price) * scope.row.num}}</p>
            <p style="color:red;" v-else>￥{{ parseFloat(scope.row.price) * scope.row.num }}.00</p>
        </template>
        </el-table-column>
      </el-table>
    </main>
    <footer>
      <div class="orderbox">
        <div class="orderinfo">
          <p>
          <span class="orderinfotitle">实付款：</span><span style="color: rgb(255, 0, 54);font: 700 26px tahoma;" v-if="price.indexOf('.') !== -1">￥{{ price }}</span><span style="color: rgb(255, 0, 54);font: 700 26px tahoma;" v-else>￥{{ price }}.00</span><br>
          </p>
          <span class="orderinfotitle">寄送至：</span><span class="orderinfodetail">{{ choseAddress.address }}</span><br>
          <span class="orderinfotitle">收件人：</span><span class="orderinfodetail">{{ choseAddress.name + choseAddress.phone}}</span><br>
        </div>
        <div>
          <button class="orderbtn" @click="commitOrder">提交订单</button>
        </div>
        <el-dialog
          title="请扫码支付"
          :visible.sync="orderdialog"
          width="40%"
          :modal-append-to-body="false"
          :lock-scroll="false"
          :before-close="handleClose">
          <div id="qrcode" ref="qrcoderef">
          </div>
          <span slot="footer" class="dialog-footer">
            <el-button @click="confimOrderFN('1')">取 消</el-button>
            <el-button type="primary" @click="confimOrderFN('0')">已扫描，支付成功</el-button>
          </span>
        </el-dialog>
      </div>
    </footer>
  </div>
</template>

<script>
import { addAddressAPI, getAddressAPI, getAddressByidAPI, deleteaddressAPI } from '@/api'
import QRCode from 'qrcodejs2'
import { mapMutations } from 'vuex'
export default {
  name: 'store-settlement',
  data () {
    return {
      addressList: [],
      choseAddress: {},
      activeId: '',
      dialogFormVisible: false,
      formLabelWidth: '120px',
      addressForm: {
        name: '',
        phone: '',
        address: ''
      },
      addressRules: {
        name: [
          { required: true, message: '请输入收件人', trigger: 'blur' }
        ],
        phone: [
          { required: true, message: '请输入收件号码', trigger: 'blur' },
          { pattern: /^1[3|4|5|6|7|8|9][0-9]{9}$/, message: '请输入正确手机号', trigger: 'blur' }
        ],
        address: [
          { required: true, message: '请输入收件地址', trigger: 'blur' }
        ]
      },
      // 支付二维码的对话框
      orderdialog: false,
      qrcode: ''
    }
  },
  computed: {
    orderList () {
      return this.$store.state.orderList
    },
    price () {
      return this.$store.state.price
    }
  },
  mounted () {
    this.getAddressList()
  },
  methods: {
    // 获取收获地址
    getAddressList () {
      getAddressAPI().then(({ data }) => {
        if (data.status !== 0) return this.$message.error(data.message)
        this.addressList = data.data
        this.activeId = this.addressList[0].id
        this.getdefaultAddress()
      })
    },
    // 初始化地址
    getdefaultAddress () {
      getAddressByidAPI(this.activeId).then(({ data }) => {
        if (data.status !== 0) return this.$message.error(data.message)
        this.choseAddress = data.data
      })
    },
    changeclass (id) {
      this.activeId = id
      getAddressByidAPI(id).then(({ data }) => {
        if (data.status !== 0) return this.$message.error(data.message)
        this.choseAddress = data.data
      })
    },
    // 删除收货地址
    deleteaddressFn (id) {
      deleteaddressAPI(id).then(({ data }) => {
        if (data.status !== 0) return this.$message.error(data.message)
        this.$message.success(data.message)
        this.getAddressList()
      })
    },
    addaddressFn () {
      this.dialogFormVisible = true
    },
    dialogCloseFN () {
      this.$refs.addref.resetFields()
    },
    // 确定提交收货地址
    cofirmCommit () {
      this.$refs.addref.validate(valid => {
        if (valid) {
          addAddressAPI(this.addressForm).then(({ data }) => {
            if (data.status !== 0) return this.$message.error(data.message)
            this.$message.success(data.message)
            this.dialogFormVisible = false
            this.getAddressList()
          })
        }
      })
    },
    gobackFn () {
      this.$router.go(-1)
    },
    ...mapMutations(['getMyorder']),
    // 提交订单按钮
    commitOrder () {
      this.orderdialog = true
      this.$nextTick(() => {
        this.qrcode = new QRCode(this.$refs.qrcoderef, {
          text: '支付页',
          width: 200,
          height: 200,
          colorDark: '#000000',
          colorLight: '#ffffff',
          correctLevel: QRCode.CorrectLevel.H
        })
      })
    },
    confimOrderFN (str) {
      if (str === '0') {
        // const date = moment().format('YYYY-MM-DD HH:mm')
        const date = new Date().getTime()
        console.log(date)
        this.orderList.forEach(item => {
          item.ordertime = date
          item.status = false
        })
        this.getMyorder(this.orderList)
        this.$notify({
          title: '支付成功',
          message: '商品购买成功',
          type: 'success'
        })
        this.$refs.qrcoderef.innerHTML = ''
        this.orderdialog = false
        this.$router.push('/myorder')
      } else {
        this.$notify({
          title: '支付失败',
          message: '商品购买失败',
          type: 'error'
        })
        this.$refs.qrcoderef.innerHTML = ''
        this.orderdialog = false
      }
    },
    handleClose () {
      this.$refs.qrcoderef.innerHTML = ''
      this.orderdialog = false
    }
  }
}
</script>

<style scoped>
  .goback {
    margin: 10px;
    padding: 0;
  }
  .goback:hover {
    opacity: .6;
    color: gray;
    cursor: pointer;
  }
  .box {
    position: absolute;
    left: 50%;
    transform: translate(-50%);
    width: 75%;
    border: 1px solid lightgray;
    background-color: #fff;
    padding-left: 20px;
    border-radius: 10px;
  }
  .title {
    font-weight: 700;
    font-size: 14px;
    color: #333;

  }
  .address {
    display: inline-block;
    position:relative;
    width: 210px;
    height: 90px;
    font-size: 12px;
    padding: 18px;
    background: url(../../assets/orderaddressno.png) no-repeat;
    cursor: pointer;
    margin-right: 10px;
    vertical-align: top;
  }
  .address:hover {
    background: url(../../assets/orderaddress.png) no-repeat;
  }
  .deleteicon {
    position:absolute;
    right: 18px;
    top: 10px;
    display: none;
  }
  .address:hover .deleteicon {
    display: block;
  }
  .addressactive{
    background: url(../../assets/orderaddress.png) no-repeat;
  }
  .imgaddress {
    width: 50px;
    height: 50px;
  }
  .addaddress {
    padding-left: 90px;
  }
  .addaddress p{
    margin: 2px;
    margin-left: -19px;
    padding: 0;
    font-size: 14px;
  }
  .shoppingcatstoreImg {
    display: inline-block;
    width: 120px;
    height: 120px;
    margin-right: 10px;
  }
  .storeinfo {
    position:absolute;
    top: 20;
  }
  .orderbox{
    float: right;
  }

  .orderinfo {
    border: 1px solid #ff0036;
    text-align: right;
    width: 320px;
    height: 90px;
    margin:5px 30px;
    box-shadow: 1px 1px rgba(0, 0, 0, 0.5);
    padding: 15px;
  }
  .orderinfo p {
    margin-top:0 ;
  }
  .orderinfotitle {
    font-weight: 700;
    color: #333;
    font-size: 10px;
  }
  .orderinfodetail {
    font-size: 10px;
  }
  .gobackCar {
    color: #36c;
    margin-right: 50px;
    font-size: 12px;
    margin: 50px 0px;
  }
  .orderbtn{
    width: 182px;
    height: 39px;
    vertical-align: middle;
    line-height: 39px;
    cursor: pointer;
    text-align: center;
    font-size: 14px;
    font-weight: 700;
    text-decoration: none;
    color: #fff;
    background-color: rgb(255, 0, 54);
    border: none;
    float: right;
    margin:5px 30px 20px 0px;
  }
  #confirm-btn {
    position: absolute;
    bottom: 20px;
    left: 50%;
    transform: translateX(-50%);
  }
</style>
